﻿Imports IETracker.UI.Process.PhoneClassLibrary.IETrackerServiceReference
Imports System.Threading
Imports System.Globalization

Partial Public Class App
    Inherits Application
    Private Shared _viewModel As MainViewModel = Nothing
    Private ReadOnly ModelKey As String = "Key"



    Public Shared _selectedTransaction As TransactionList = Nothing


    ''' <summary>
    ''' A static ViewModel used by the views to bind against.
    ''' </summary>
    ''' <returns>The MainViewModel object.</returns>
    Public Shared Property ViewModel() As MainViewModel
        Get
            ' Delay creation of the view model until necessary
            If _viewModel Is Nothing Then
                _viewModel = New MainViewModel()
            End If

            Return _viewModel
        End Get
        Private Set(value As MainViewModel)
            _viewModel = value
        End Set

    End Property


    ''' <summary>
    ''' Provides easy access to the root frame of the Phone Application.
    ''' </summary>
    ''' <returns>The root frame of the Phone Application.</returns>
    Public Property RootFrame() As PhoneApplicationFrame

    ''' <summary>
    ''' Constructor for the Application object.
    ''' </summary>
    Public Sub New()
        ' Standard Silverlight initialization
        InitializeComponent()

        ' Phone-specific initialization
        InitializePhoneApplication()

        'Thread.CurrentThread.CurrentCulture = New CultureInfo("nl-NL")
        Thread.CurrentThread.CurrentCulture = DirectCast(Thread.CurrentThread.CurrentCulture.Clone(), CultureInfo)
        Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"

        ' Show graphics profiling information while debugging.
        If System.Diagnostics.Debugger.IsAttached Then
            ' Display the current frame rate counters.
            Application.Current.Host.Settings.EnableFrameRateCounter = True

            ' Show the areas of the app that are being redrawn in each frame.
            'Application.Current.Host.Settings.EnableRedrawRegions = True

            ' Enable non-production analysis visualization mode, 
            ' which shows areas of a page that are handed off to GPU with a colored overlay.
            'Application.Current.Host.Settings.EnableCacheVisualization = True


            ' Disable the application idle detection by setting the UserIdleDetectionMode property of the
            ' application's PhoneApplicationService object to Disabled.
            ' Caution:- Use this under debug mode only. Application that disable user idle detection will continue to run
            ' and consume battery power when the user is not using the phone.
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled
        End If

    End Sub

    ' Code to execute when the application is launching (eg, from Start)
    ' This code will not execute when the application is reactivated
    Private Sub Application_Launching(ByVal sender As Object, ByVal e As LaunchingEventArgs)
        If Not App.ViewModel.IsDataLoaded Then
            App.ViewModel.LoadData()
            RootFrame.DataContext = ViewModel
        End If
    End Sub

    ' Code to execute when the application is activated (brought to foreground)
    ' This code will not execute when the application is first lau5nched
    Private Sub Application_Activated(ByVal sender As Object, ByVal e As ActivatedEventArgs)
        ' Ensure that application state is restored appropriately
        
        If PhoneApplicationService.Current.State.ContainsKey(ModelKey) Then
            ViewModel = TryCast(PhoneApplicationService.Current.State(ModelKey), MainViewModel)
            RootFrame.DataContext = ViewModel
        Else
            If Not App.ViewModel.IsDataLoaded Then
                App.ViewModel.LoadData()
            End If
        End If

    End Sub

    ' Code to execute when the application is deactivated (sent to background)
    ' This code will not execute when the application is closing
    Private Sub Application_Deactivated(ByVal sender As Object, ByVal e As DeactivatedEventArgs)
        ' Ensure that required application state is persisted here.
        PhoneApplicationService.Current.State(ModelKey) = ViewModel
    End Sub

    ' Code to execute when the application is closing (eg, user hit Back)
    ' This code will not execute when the application is deactivated
    Private Sub Application_Closing(ByVal sender As Object, ByVal e As ClosingEventArgs)
    End Sub

    ' Code to execute if a navigation fails
    Private Sub RootFrame_NavigationFailed(ByVal sender As Object, ByVal e As NavigationFailedEventArgs)
        If System.Diagnostics.Debugger.IsAttached Then
            ' A navigation has failed; break into the debugger
            System.Diagnostics.Debugger.Break()
        End If
    End Sub

    ' Code to execute on Unhandled Exceptions
    Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles Me.UnhandledException
        If System.Diagnostics.Debugger.IsAttached Then
            ' An unhandled exception has occurred; break into the debugger
            System.Diagnostics.Debugger.Break()
        End If
    End Sub

#Region "Phone application initialization"

    ' Avoid double-initialization
    Private phoneApplicationInitialized As Boolean = False

    ' Do not add any additional code to this method
    Private Sub InitializePhoneApplication()
        If phoneApplicationInitialized Then
            Return
        End If

        ' Create the frame but don't set it as RootVisual yet; this allows the splash
        ' screen to remain active until the application is ready to render.
        RootFrame = New PhoneApplicationFrame()
        AddHandler RootFrame.Navigated, AddressOf CompleteInitializePhoneApplication

        ' Handle navigation failures
        AddHandler RootFrame.NavigationFailed, AddressOf RootFrame_NavigationFailed

        ' Ensure we don't initialize again
        phoneApplicationInitialized = True
    End Sub

    ' Do not add any additional code to this method
    Private Sub CompleteInitializePhoneApplication(ByVal sender As Object, ByVal e As NavigationEventArgs)
        ' Set the root visual to allow the application to render
        If RootVisual IsNot RootFrame Then
            RootVisual = RootFrame
        End If

        ' Remove this handler since it is no longer needed
        RemoveHandler RootFrame.Navigated, AddressOf CompleteInitializePhoneApplication
    End Sub

#End Region



End Class